package com.kxf.androidtestdemo.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.LinearLayout;

import com.kxf.androidtestdemo.R;
import com.kxf.androidtestdemo.chart.ChartService;

import org.achartengine.GraphicalView;

import java.util.Timer;
import java.util.TimerTask;

public class AChartEngineActivity extends BaseActivity {

    private LinearLayout ll1;
    private ChartService mService;
    private GraphicalView mView;

    private int t = 0;
    private Handler handler = new Handler() {
        @Override
        //定时更新图表
        public void handleMessage(Message msg) {
            mService.updateChart(t, Math.random() * 10+70);
            t+=5;
        }
    };
    private Timer timer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_a_chart_engine);

        ll1 = findViewById(R.id.ll1);
        mService = new ChartService(this);
        mService.setXYMultipleSeriesDataset("左温度曲线");
        mService.setXYMultipleSeriesRenderer(400, 100, "左温度曲线", "时间", "温度",
                Color.RED, Color.RED, Color.RED, Color.BLACK);
        mView = mService.getGraphicalView();

        ll1.addView(mView, new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));

        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                handler.sendMessage(handler.obtainMessage());
                if (t>2000){
                    timer.cancel();
                }
            }
        }, 10, 100);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timer != null) {
            timer.cancel();
        }
    }
}